package com.example.demo.controller;

import com.example.demo.dao.Creation;
import com.example.demo.dao.SysRoleMenuDao;
import com.example.demo.domain.SysMenu;
import com.example.demo.domain.SysRoleMenu;
import com.example.demo.domain.SysUser;
import com.example.demo.service.SysMenuService;
import com.example.demo.service.SysRoleMenuService;
import com.example.demo.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.example.demo.dao.Creation.Operation.*;

@Controller
public class HomeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @GetMapping("/")
    public ModelAndView login(){
        return new ModelAndView("redirect:/login.html");
    }

    @RequestMapping("/main")
    public String main(){
        return "home/main";
    }

    @PostMapping("/login/")
    @ResponseBody
    public ModelMap login(String userName, String password, HttpServletRequest request){
        ModelMap modelMap = new ModelMap();
        List<SysUser> list = sysUserService.search(new Creation("user_name", eq, userName),
                new Creation("password", eq, password));

        if(list.isEmpty()){
            modelMap.addAttribute("success", false);
            modelMap.addAttribute("msg", "用户名或密码不正确");
            return modelMap;
        }

        SysUser sysUser = list.get(0);
        request.getSession().setAttribute("user", sysUser);

        List<Map<String, Object>> sysMenus = sysRoleMenuDao.selectMenusByRoleId(sysUser.getRoleId());




        Map<String, List<Map>> subMenus = sysMenus.stream().filter( x -> {
            return sysUser.getRoleId().equals(x.get("role_id"));
        }).filter(x -> {
            return 2 == (Integer)x.get("level");
        }).collect(Collectors.groupingBy(x -> (String)x.get("parent_id")));

        Set<String> parentIds = subMenus.keySet();

        List<Map> parentMenus = sysMenus.stream().filter(x -> {
            return 1 == (Integer)x.get("level");
        }).filter(x -> {
            return parentIds.contains(x.get("id"));
        }).peek(x -> {
            x.put("children", subMenus.get(x.get("id")));
        }).collect(Collectors.toList());

        request.getSession().setAttribute("menus", parentMenus);
        modelMap.addAttribute("success", true);


        return modelMap;
    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return new ModelAndView("redirect:/login.html");
    }

    @RequestMapping("/test/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/test2")
    public String test2(){
        return "test2";
    }

    @RequestMapping("/test3")
    @ResponseBody
    public ResponseEntity test3(HttpServletRequest request){
        String content = request.getParameter("content");
        logger.debug(content);
        logger.debug("length:{}", null == content ? 0 : content.length());
        return ResponseEntity.ok("success");
    }

    @RequestMapping("/test4")
    public String test4(){
        return "test4";
    }

    @RequestMapping("/test5")
    public String test5(){
        return "test5";
    }
}
